package com.sykj.weichat.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.stat.TableStat.Mode;
import com.sykj.weichat.model.BisNurseProject;
import com.sykj.weichat.model.BisShop;
import com.sykj.weichat.model.BisShopCollect;
import com.sykj.util.ResultBody;
import com.sykj.weichat.service.BisNurseProjectService;
import com.sykj.weichat.service.BisShopCollecttService;
import com.sykj.weichat.service.BisShopService;
/**
 * 
 * 类名:com.sykj.weichat.web.WeichatShopController
 * 描述:微信店铺相关处理
 * 编写者:张孝柱
 * 创建时间:2017年9月18日
 * 修改说明:
 */

@Controller
@RequestMapping("weichat/shop")
public class WeichatShopController {
	@Autowired
	BisShopCollecttService BisShopCollecttService;
	@Autowired
	BisShopService bisshopservice;
	@Autowired
	BisNurseProjectService BisNurseProjectService;
	/**
	 * 
	 * 简要说明：查看我的收藏
	 * 编写者：张孝柱
     * 创建时间:2017年9月18日
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping("listShopCollect")
	public String listShopCollect(HttpSession session,Model model){
		String custom_user_id = (String) session.getAttribute("custom_user_id");
		List<BisShopCollect> getall = BisShopCollecttService.getall(custom_user_id);
		if (getall.size()>0) {
			model.addAttribute("shopList", getall);
			model.addAttribute("show_data", "1");
		}else {
			model.addAttribute("show_data", "0");
		}
	
		return "weichat/my/myShopCollect.jsp";
		
	}
	/**
	 * 跳到找店铺页面
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "goFindShop")
	public String goFindShop(String returnType,String project_id,Model model){
		if (returnType.equals("1")) {
			model.addAttribute("project_id", project_id);
			return "weichat/project/selectShop.jsp";
		}else {
			return "weichat/shop/findShop.jsp";
		}
	}
	/**
	 * 
	 * 简要说明：搜索商店
	 * 编写者：张孝柱
     * 创建时间:2017年9月18日
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping(value = "searchShop")
	@ResponseBody
	public ResultBody searchShop(){
		List<BisShop> getall = bisshopservice.getall();
		return ResultBody.getData(getall.size(), getall, "");
		
	}
	/**
	 * 
	 * 简要说明：查看店铺详情
	 * 编写者：张孝柱
     * 创建时间:2017年9月19日
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping("showShopDetail")
	public String showShopDetail(String shop_id,HttpSession session,BisShopCollect bisShopCollect,Model model){
		String custom_user_id = (String) session.getAttribute("custom_user_id");
		bisShopCollect.setShop_id(shop_id);
		bisShopCollect.setCustom_user_id(custom_user_id);
		int queryShopCollectCount = BisShopCollecttService.queryShopCollectCount(bisShopCollect);
		model.addAttribute("count", queryShopCollectCount);
		BisShop getuser = bisshopservice.getuser(shop_id);
		model.addAttribute("shopPO", getuser);
		List<BisNurseProject> getall = BisNurseProjectService.getall();
		model.addAttribute("projectList", getall);
		return "weichat/shop/showShopDetail.jsp";
		
	}
	/**
	 * 
	 * 简要说明：店铺收藏
	 * 编写者：张孝柱
     * 创建时间:2017年9月27日
	 * @param 说明
	 * @return 说明
	 */
	@RequestMapping("saveShopCollect")
	@ResponseBody
	public String saveShopCollect(String shop_id,BisShopCollect bisShopCollect,HttpSession session){
		String result = "{\"appcode\":\"%s\", \"appmsg\":\"%s\", \"showClass\":\"%s\" }";
		String custom_user_id = (String) session.getAttribute("custom_user_id");
		bisShopCollect.setShop_id(shop_id);
		bisShopCollect.setCustom_user_id(custom_user_id);
		int count = BisShopCollecttService.queryShopCollectCount(bisShopCollect);
		if (count>0) {
			int delete = BisShopCollecttService.delete(shop_id);
			if (delete>0) {
				return String.format(result,"1","取消成功！","1");
			}else {
				return String.format(result,"0","取消失败！","0");
			}
		}else {
			bisShopCollect.setShop_id(shop_id);
			bisShopCollect.setCustom_user_id(custom_user_id);
			bisShopCollect.setCreate_time(new Date());
			int insert = BisShopCollecttService.insert(bisShopCollect);
			if (insert>0) {
				return String.format(result,"1","收藏成功！","1");
			}else {
				return String.format(result,"0","收藏失败！","1");
			}
		}
	}
}
